#include "editprofile.h"
#include "ui_editprofile.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDriver>
#include <QFileDialog>
#include <QFile>

Editprofile::Editprofile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Editprofile)
{
    ui->setupUi(this);
}

Editprofile::~Editprofile()
{
    delete ui;
}

void Editprofile::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(nullptr, "Выберите файл", ".", "Изображение (*.jpeg *.png *.jpga *.jpg)");
    QFile* imageFile = new QFile(filename);
    ui->ImageSelector->setPixmap(filename);
    imageFile->open(QIODevice::ReadOnly);
    imageByte = imageFile->readAll();
    imageFile->close();
    delete imageFile;
}

void Editprofile::editData(const QString &name, const QString &surname, const QString &patronymic, const QString &number, const QString &role)
{
    ui->namelineEdit->setText(name);
    qDebug() << name;
    qDebug() << role;
    qDebug() << number;
}

void Editprofile::on_addButton_clicked()
{
    QSqlQuery query;
        query.prepare("INSERT INTO \"User\" (\"Name\", \"Surname\", \"Patronymic\", \"Login\", \"Password\",  \"Phone_number\", \"role_id\", \"image\") "
            "VALUES (:Name, :Surname, :Patronymic, :Login, :Password,  :Phone_number, :role_id, :Image)");
        query.bindValue(":Name", ui->namelineEdit->text());
        query.bindValue(":Surname", ui->surnamelineEdit->text());
        query.bindValue(":Patronymic", ui->patronymicLineEdit->text());
        query.bindValue(":Login", ui->loginlineEdit->text());
        query.bindValue(":Password", ui->passwordlineEdit->text());
        query.bindValue(":Phone_number", ui->numberlineEdit->text());
        if(ui->roleComboBox->currentText()=="Админ")
            query.bindValue(":role_id", 1);
        else
            query.bindValue(":role_id", 2);
        query.bindValue(":Image", imageByte);
        query.exec();
}



